package dataflowscheme.engine.core;

import org.apache.log4j.Logger;

/**
 * Synchro event is parsed by Event Parser Thread and it is 
 * intended to send signal token between to processing entities, 
 * Trigger and Stopper
 * 
 * @author <a href="mailto:misiak7@gmail.com">Michal Antolik</a>
 *
 */
public class SynchroEvent implements IEvent{
	
	private final ConnectionAnchor in;
	private final ConnectionAnchor out;
	
	private Logger log = Logger.getLogger(SynchroEvent.class);
	
	public SynchroEvent(ConnectionAnchor out, ConnectionAnchor in) {
		this.in = in;
		this.out = out;
	}

	/* (non-Javadoc)
	 * @see dataflowscheme.engine.core.IEvent#execute()
	 */
	public void execute() {
		
		log.debug("Sending synchro token from '" + out.pe.getName() + "/" + out.portName + "' to '"+ 
				in.pe.getName() + "/" + in.portName + "'.");
		
		if(in.pe.receiveSignal(in.portName)){
			log.debug("Synchro token was delivered successfully");			
			out.pe.receiveResponse(EventMsg.delivered_ok, out.portName);
		} else {
			//discard signal event
			log.debug("Synchro token was NOT delivered successfully");
		}
	}
}
